package com.mhy.crash;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.mhy.crash.utils.HYCrashUtils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {
    static {
        System.loadLibrary("native-lib");
    }
    public native void nativeCrash();
    TextView tv_buildType;
    ExecutorService service = Executors.newSingleThreadExecutor();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            //tv_buildType.setText("子线程非法操作ui");
            Looper.prepare();
            tv_buildType.setText("子线程合法操作ui");
            Looper.loop();

            float o = Integer.valueOf("fg%~`]'9&*(/0");
            tv_buildType.setText("Crash后的方法不会执行了");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.btn_crash);
        Button button1 = findViewById(R.id.btn_show);
        Button button3 = findViewById(R.id.btn_show3);
        Button button2 = findViewById(R.id.btn_show2);
        Button jniCrash = findViewById(R.id.jniCrash);
        Button btn_clear = findViewById(R.id.btn_clear);
        tv_buildType = findViewById(R.id.tv_buildType);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = null;//自动捕获
                text.toUpperCase();
            }
        });
        jniCrash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nativeCrash();
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = null;
//                text.toUpperCase();
//                button.setText("Crash后的方法不会执行了");
                try {
                    text.toUpperCase();
                    button.setText("Crash后的方法不会执行了");
                } catch (Exception e) {//自定义捕获
                    HYCrash.getInstance(MainActivity.this).showCrashActivity(e);
                }
            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                service.submit(runnable);
                service.execute(runnable);
            }
        });


        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, MainAct.class));
            }
        });
        btn_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HYCrashUtils.clearCrashCache(MainActivity.this);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        service.shutdown();
    }
}
